<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/homePage' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>租赁管理</el-breadcrumb-item>
                <el-breadcrumb-item>车辆配置</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>

            <el-form :inline="true" :model="queryCarConfigData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="queryCarConfigData.typename" placeholder="请输入类型名"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-input v-model="queryCarConfigData.speedBox" placeholder="请输入变速箱"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-input v-model="queryCarConfigData.seating" placeholder="请输入座位数"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-input v-model="queryCarConfigData.airSuction" placeholder="请输入进气方式"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="showAllCarConfig">查询</el-button>
                    <el-button type="primary" @click="showAddForm({})">添加</el-button>
                </el-form-item>
            </el-form>

            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="汽车类配置id">
                    </el-table-column>
                    <el-table-column
                            prop="typename"
                            label="汽车配置名">
                    </el-table-column>
                    <el-table-column
                            prop="speedBox"
                            label="变速箱">
                    </el-table-column>
                    <el-table-column
                            prop="tankCapacity"
                            label="油箱容量">
                    </el-table-column>
                    <el-table-column
                            prop="displacement"
                            label="排量">
                    </el-table-column>
                    <el-table-column
                            prop="fcphk"
                            label="百公里油耗">
                    </el-table-column>
                    <el-table-column
                            prop="seating"
                            label="座位数">
                    </el-table-column>
                    <el-table-column
                            prop="airSuction"
                            label="进气方式">
                    </el-table-column>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <!-- 修改 -->
                            <el-button type="primary" icon="el-icon-edit" circle @click="showAddForm(scope.row)"></el-button>
                            <!-- 删除 -->
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteCarConfigById(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 7, 9]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>

                <el-dialog
                        title="新增车辆配置"
                        :visible.sync="addForm"
                        width="30%">
                <span>
                    <!-- 新增车辆信息 -->
                    <el-form ref="configForm" :model="addCarConfigForm" :rules="rules" label-position="left" label-width="80px">
                        <el-input v-model="addCarConfigForm.id" type="hidden"></el-input>
                        <el-form-item label="配置名称" prop="typename">
                            <el-input v-model="addCarConfigForm.typename"></el-input>
                        </el-form-item>
                        <el-form-item label="变速箱" prop="speedBox">
                            <el-input v-model="addCarConfigForm.speedBox"></el-input>
                        </el-form-item>
                        <el-form-item label="油箱容量" prop="tankCapacity">
                            <el-input v-model="addCarConfigForm.tankCapacity"></el-input>
                        </el-form-item>
                        <el-form-item label="排量" prop="displacement">
                            <el-input v-model="addCarConfigForm.displacement"></el-input>
                        </el-form-item>
                        <el-form-item label="百公里油耗" prop="fcphk">
                            <el-input v-model="addCarConfigForm.fcphk"></el-input>
                        </el-form-item>
                        <el-form-item label="座位数" prop="seating">
                            <el-input v-model.number="addCarConfigForm.seating"></el-input>
                        </el-form-item>
                        <el-form-item label="进气方式" prop="airSuction">
                            <el-input v-model="addCarConfigForm.airSuction"></el-input>
                        </el-form-item>
                    </el-form>
                </span>
                    <span slot="footer" class="dialog-footer">
                    <el-button @click="addForm = false">取 消</el-button>
                    <el-button type="primary" @click="addCarConfig('configForm')">确 定</el-button>
                    </span>
                </el-dialog>
            </template>

        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                queryCarConfigData: {
                    typename: '',
                    speedBox: '',
                    seating: '',
                    airSuction: ''
                },
                tableData: [],
                addCarConfigForm: {
                    id: '',
                    typename: '',
                    speedBox: '',
                    tankCapacity: '',
                    displacement: '',
                    fcphk: '',
                    seating: '',
                    airSuction: ''
                },
                rules: {
                    typename: [
                        { required: true, message: '请输入配置名称', trigger: 'blur' }
                    ],
                    speedBox: [
                        { required: true, message: '请输入变速箱类型', trigger: 'blur' }
                    ],
                    tankCapacity: [
                        { required: true, message: '请输入邮箱容量', trigger: 'blur' }
                    ],
                    displacement: [
                        { required: true, message: '请输入排量', trigger: 'blur' }
                    ],
                    fcphk: [
                        { required: true, message: '请输百公里油耗', trigger: 'blur' }
                    ],
                    seating: [
                        { required: true, message: '请输入座位数', trigger: 'blur' },
                        { type: 'number', message: '座位数必须为数字值',trigger: 'blur'}
                    ],
                    airSuction: [
                        { required: true, message: '请输入进气方式', trigger: 'blur' }
                    ]
                },
                addForm : false,
                pageSize: 5,
                currentPage: 1,
                total: 0,
            }
        },
        methods:{
            handleSizeChange(value) {
                this.pageSize = value;
                this.showAllCarConfig();
            },
            handleCurrentChange(value) {
                this.currentPage = value;
                this.showAllCarConfig();
            },
            // 查询全部的汽车配置
            showAllCarConfig() {
                this.$axios({
                    method: 'get',
                    url: 'clms-lease/carconfig/showAllCarConfig',
                    params: {
                        pageno: this.currentPage,
                        pagesize: this.pageSize,
                        typename: this.queryCarConfigData.typename,
                        speedBox: this.queryCarConfigData.speedBox,
                        seating: this.queryCarConfigData.seating,
                        airSuction: this.queryCarConfigData.airSuction
                    }
                }).then( result => {
                    if (result.data.code == 200) {
                        this.tableData = result.data.listData;
                        this.total = result.data.object.total;
                    }
                })
            },
            // 显示新增的form表单
            showAddForm(row) {
                this.addForm = true;
                this.addCarConfigForm = row;
            },
            // 新增车辆配置
            addCarConfig(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var seating = parseInt(this.addCarConfigForm.seating);
                        var tankCapacity = parseInt(this.addCarConfigForm.tankCapacity);
                        var fcphk = parseInt(this.addCarConfigForm.fcphk);
                        if (seating >0 && tankCapacity >0 && fcphk>0) {
                            if (this.addCarConfigForm.id == null) {
                                this.$axios({
                                    method: 'post',
                                    url: 'clms-lease/carconfig/addCarConfig',
                                    data : this.addCarConfigForm
                                }).then( result => {
                                    if (result.data.code == 200) {
                                        this.addForm = false;
                                        this.$message.success(result.data.message);
                                        this.showAllCarConfig()
                                    } else {
                                        this.$message.error(result.data.message)
                                    }
                                })
                            } else {
                                this.$axios({
                                    method: 'post',
                                    url: 'clms-lease/carconfig/updateCarConfig',
                                    data : this.addCarConfigForm
                                }).then( result => {
                                    if (result.data.code == 200) {
                                        this.addForm = false;
                                        this.$message.success(result.data.message);
                                        this.showAllCarConfig()
                                    } else {
                                        this.$message.error(result.data.message)
                                    }
                                })

                            }
                        } else {
                            this.$message.error("数据输入有误")
                        }
                    } else {
                        this.$message.error("数据输入有误")
                    }
                });
            },
            // 删除汽车配置
            deleteCarConfigById(id) {
                this.$confirm('确定删除此配置吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios({
                        method: 'get',
                        url: 'clms-lease/carconfig/deleteCarConfigById',
                        params: {
                            id: id
                        }
                    }).then( result => {
                        if (result.data.code == 200) {
                            this.$message.success(result.data.message);
                            this.currentPage = 1;
                            this.showAllCarConfig()
                        } else {
                            this.$message.error(result.data.message)
                        }
                    })
                });
            }
        },
        created() {
            this.showAllCarConfig()
        }
    }
</script>